package it.uniroma2.orchestrator.rule;

import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;

import it.uniroma2.gestoreontologie.Gestore;
import it.uniroma2.model.wizard.Parametri;
import gnu.prolog.term.AtomTerm;
import gnu.prolog.term.CompoundTerm;
import gnu.prolog.term.Term;
import gnu.prolog.term.VariableTerm;
import gnu.prolog.vm.Environment;
import gnu.prolog.vm.Interpreter;
import gnu.prolog.vm.Interpreter.Goal;
import gnu.prolog.vm.PrologException;

public class RulePrologHandler {

	
	Environment env = new Environment();
	
	public RulePrologHandler(){
		
	}
	
	
	//This method uses prolog to select classificators
	public void executeRuleClassi(){
		
		//Load rules
		int rc;
		env.ensureLoaded(AtomTerm.get("ruleClassificator.pl"));
		env.ensureLoaded(AtomTerm.get("facts.pl"));
		Interpreter inte = env.createInterpreter(); 
		env.runInitialization(inte);
		VariableTerm type = new VariableTerm("Type");
		VariableTerm rank = new VariableTerm("Rank");
		Term[] ter =  { type , rank};
		CompoundTerm goal = new CompoundTerm(AtomTerm.get("classificator"), ter);
		try {
			inte.runOnce(goal);
			Goal goals = inte.prepareGoal(goal);
			
			rc = inte.execute(goals);
			
			System.out.println(rc);
			System.out.println(type);
			System.out.println(rank);
			rc = inte.execute(goals);
			
			System.out.println(rc);
			System.out.println(type);
			System.out.println(rank);
			
		} catch (PrologException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	//This method creates prolog facts reading the ontology
	public void createFacts(String domPath){
		
		//Create the facts file
		
		try {
		    BufferedWriter out = new BufferedWriter(new FileWriter("facts.pl"));
		    
		    Gestore gestore = new Gestore(Parametri.NS_DOM, domPath);
		    
		    //Add targets
		    Vector<String> target = gestore.cercaIndividual("Target");
		    for(String ind : target){
		    	String tmp = gestore.eliminaNs(ind);
		    	out.write("target(" + tmp +").\n");
		    	
		    }
		    
		    //Add features
		    Vector<String> feature = gestore.cercaIndividual("Feature");
		    for(String ind : feature){
		    	String tmp = gestore.eliminaNs(ind);
		    	out.write("feature(" + tmp +").\n");
		    }
		    
		    //Add polarity
		    Vector<String> polarity = gestore.cercaIndividual("Polarity");
		    for(String ind : polarity){
		    	String tmp = gestore.eliminaNs(ind);
		    	out.write("polarity(" + tmp +").\n");
		    }
		    
		    //Add has_subtarget relation
		    for(String ind : target){
		    	Vector<String> prop = gestore.recuperaProprietaVect(gestore.eliminaNs(ind), "Has_Subtarget");
		    	for(String ind2 : prop){
		    		out.write("has_subtarget(" + gestore.eliminaNs(ind) + ", " + ind2 + ").\n");
		    	}
		    }
		    
		  //Add has_polarity relation
		    for(String ind : target){
		    	Vector<String> prop = gestore.recuperaProprietaVect(gestore.eliminaNs(ind), "Has_Polarity");
		    	for(String ind2 : prop){
		    		out.write("has_polarity(" + gestore.eliminaNs(ind) + ", " + ind2 + ").\n");
		    	}
		    }
		    
		  //Add has_feature relation
		    for(String ind : target){
		    	Vector<String> prop = gestore.recuperaProprietaVect(gestore.eliminaNs(ind), "Has_Feature");
		    	for(String ind2 : prop){
		    		out.write("has_feature(" + gestore.eliminaNs(ind) + ", " + ind2 + ").\n");
		    	}
		    }
		    
		    out.close();
		} catch (IOException e) {
		}
		
		
		
		
		
	}
}
